import http from "../utils/request"
import {PageRequestType, ResponseType, PageResponseType} from "../types/Response";
import {AxiosResponse} from "axios";

const apiPrefix:string = "/user";

export interface UserType {
    // id
    id?:string|null|undefined
    // 用户名
    username?:string|null|undefined
    // 密码,passwordEncoder加密
    password?:string|null|undefined
    // 邮箱
    mail?:string|null|undefined
    // 用户角色(1普通用户,2企业用户)
    role_id?:number|null|undefined
    // 手机号
    phone?:string|null|undefined
    // 年龄
    age?:number|null|undefined
    // 性别(1男,2女)
    gender?:number|null|undefined
    // 地址
    place?:string|null|undefined
    // 创建时间
    create_at?:any|null|undefined
    // 修改时间
    update_at?:any|null|undefined
}

export interface UserPageSearchType extends PageRequestType, UserType {

}


export async function addUser(data: UserType): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix,
        method: 'post'
    })
}

export async function deleteUserById(id: string): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            id
        },
        url: apiPrefix+'/id',
        method: 'delete'
    })
}

export async function deleteUserByIds(ids: string[]): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        params: {
            ids
        },
        url: apiPrefix+'/batch',
        method: 'delete'
    })
}


export async function searchUserById(id: string): Promise<AxiosResponse<ResponseType<UserType>>> {
    return await http.request({
        params: {
            id
        },
        url: apiPrefix+'/id',
        method: 'get'
    })
}

export async function searchUser(params: UserType): Promise<AxiosResponse<ResponseType<UserType[]>>> {
    return await http.request({
        params: params,
        url: apiPrefix,
        method: 'get'
    })
}

export async function searchUserByPage(params: UserPageSearchType): Promise<AxiosResponse<ResponseType<PageResponseType<UserType>>>> {
    return await http.request({
        params: params,
        url: apiPrefix+'/page',
        method: 'get'
    })
}


export async function updateUserById(data: UserType): Promise<AxiosResponse<ResponseType<null>>> {
    return await http.request({
        data: data,
        url: apiPrefix+'/changeUserInfo',
        method: 'put'
    })
}
